package com.example.dqhl.jdmetro;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.fragment.ConsultationFragment;
import com.example.dqhl.jdmetro.fragment.LifeFragment;
import com.example.dqhl.jdmetro.fragment.LiveFragment;
import com.example.dqhl.jdmetro.fragment.PersonalFragment;
import com.example.dqhl.jdmetro.utils.NetUtils;
import com.example.dqhl.jdmetro.utils.Toastutils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @Bind(R.id.rg_menu)
    RadioGroup rg_menu;
    @Bind(R.id.rb_consultation)
    RadioButton rb_consultation;//咨询
    @Bind(R.id.rb_life)
    RadioButton rb_life;//生活
    @Bind(R.id.rb_live)
    RadioButton rb_live;//直播
    @Bind(R.id.rb_personal)
    RadioButton rb_personal;//个人

    private boolean quit = false; //设置退出标识
    private ConsultationFragment consultationFragment;
    private LifeFragment lifeFragment;
    private LiveFragment liveFragment;
    private PersonalFragment personalFragment;

    private static final int CONSULATATION = 0; //咨询
    private static final int LIVE = 1; //直播
    private static final int LIFE = 2; //生活
    private static final int PERSONAL = 3; //个人
    private Context context;
    private FragmentManager manager;

    @Override
    public void initView(Bundle savedInstanceState) {
        context = this;
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        showFragment(CONSULATATION);
        manager = getSupportFragmentManager();
        if (!NetUtils.isConnected(context)) {
            Toastutils.show("网络连接异常");
        }
    }


    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        rg_menu.setOnCheckedChangeListener(this);
    }


    @Override
    public void onBackPressed() {
        if (!quit) { //询问退出程序
            Toastutils.showLong(R.string.txt_toast_quit_app);
            new Timer(true).schedule(new TimerTask() { //启动定时任务
                @Override
                public void run() {
                    quit = false; //重置退出标识
                }
            }, 2000);
            quit = true;
        } else { //确认退出程序
            super.onBackPressed();
        }
    }

    /**
     * 显示相应的fragment
     *
     * @param index
     */
    public void showFragment(int index) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        hideFragment(ft);
        switch (index) {
            case CONSULATATION:
                if (consultationFragment == null) {
                    consultationFragment = new ConsultationFragment();
                    ft.add(R.id.frame_layout, consultationFragment, "ConsultationFragment");
                } else {
                    ft.show(consultationFragment);
                }
                break;
            case LIFE:
                if (lifeFragment == null) {
                    lifeFragment = new LifeFragment();
                    ft.add(R.id.frame_layout, lifeFragment, "LifeFragment");
                } else {
                    ft.show(lifeFragment);
                }
                break;
            case LIVE:
                if (liveFragment == null) {
                    liveFragment = new LiveFragment();
                    ft.add(R.id.frame_layout, liveFragment, "LiveFragment");
                } else {
                    ft.show(liveFragment);
                }
                break;
            case PERSONAL:
                if (personalFragment == null) {
                    personalFragment = new PersonalFragment();
                    ft.add(R.id.frame_layout, personalFragment, "PersonalFragment");
                } else {
                    ft.show(personalFragment);
                }
                break;
        }
        ft.commit();
    }

    /**
     * 隐藏相应的fragment
     *
     * @param ft
     */
    private void hideFragment(FragmentTransaction ft) {
        if (consultationFragment != null) {
            ft.hide(consultationFragment);
        }
        if (lifeFragment != null) {
            ft.hide(lifeFragment);
        }
        if (liveFragment != null) {
            ft.hide(liveFragment);
        }
        if (personalFragment != null) {
            ft.hide(personalFragment);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_consultation:
                showFragment(CONSULATATION);
                break;
            case R.id.rb_life:
                showFragment(LIFE);
                break;
            case R.id.rb_live:
                showFragment(LIVE);
                break;
            case R.id.rb_personal:
                showFragment(PERSONAL);
                break;
        }
    }

}
